package at.fhj.itm10.ticketmaster.helper;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import at.fhj.itm10.ticketmaster.validators.ExternalContextStub;

public abstract class ContextMocker extends FacesContext {
  private ContextMocker() {
  }

  private static final Release RELEASE = new Release();

  private static class Release implements Answer<Void> {

    public Void answer(InvocationOnMock invocation) throws Throwable {
      setCurrentInstance(null);
      return null;
    }
  }

  public static FacesContext mockFacesContext() {
    FacesContext context = Mockito.mock(FacesContext.class);
    setCurrentInstance(context);
    
   
    Mockito.doAnswer(RELEASE)
        .when(context)
        .release();
    return context;
  }

	@Override
	public ExternalContext getExternalContext() {
		// TODO Auto-generated method stub
		return new ExternalContextStub();
	}
}